package com.zdgcc.site.db;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class EchannelDateFormat {

	private static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 将现在的时间变成特定时间格式
	 * 
	 * @return
	 */
	public static String getFinanceDate() {
		String dateFormat = "yyMMdd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		return sdf.format(new Date());
	}

	/**
	 * 根据给定的时间格式得到当前时间
	 * 
	 * @param formatString
	 * @return
	 */
	public static String getCurrentDateTime(String formatString) {
		return new SimpleDateFormat(formatString).format(new Date());
	}

	/**
	 * 得到当前时间 格式为:yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentDateTime() {
		return new SimpleDateFormat(DATEFORMAT).format(new Date());
	}

	/**
	 * 将现有的时间变成特定时间格式
	 * 
	 * @param time
	 * @return
	 */
	public static String getFinanceDate(String time) {
		return time.substring(2).replaceAll("-", "");
	}

	/**
	 * 将选择的日期加1用于查询
	 * 
	 * @param time
	 * @return
	 */
	public static String addOneDay(String time) {
		Calendar c = Calendar.getInstance();
		String[] array = time.split("-");
		c.set(Integer.parseInt(array[0]), Integer.parseInt(array[1]) - 1,
				Integer.parseInt(array[2]));
		c.add(Calendar.DAY_OF_MONTH, 1);
		Date d = c.getTime();
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(d);
	}

	/**
	 * 将选择的日期减N用于查询
	 * 
	 * @param time
	 * @return
	 */
	public static String subtractOneDay(String time, int dayNUm) {
		Calendar c = Calendar.getInstance();
		String[] array = time.split("-");
		c.set(Integer.parseInt(array[0]), Integer.parseInt(array[1]) - 1,
				Integer.parseInt(array[2]));
		c.add(Calendar.DAY_OF_MONTH, dayNUm);
		Date d = c.getTime();
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(d);
	}

	/**
	 * 取当前年份当月的后一个月
	 * 
	 * @param time
	 * @return
	 */
	public static String addOneMonth(String time, String dateFormat) {
		Calendar c = Calendar.getInstance();
		String[] array = time.split("-");
		c.set(Integer.parseInt(array[0]), Integer.parseInt(array[1]), 1);
		Date d = c.getTime();
		String format = dateFormat;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(d);
	}

	/**
	 * 将给定的日期变成秒数并返回
	 * 
	 * @param formaString
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long convertDateToSecond(String formatString, String time)
			throws ParseException {
		String format = DATEFORMAT;
		if (formatString != null && !"".equals(formatString)) {
			format = formatString;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(time).getTime() / 1000;
	}

	// 判断时间date1是否在时间date2之前

	// 时间格式 2005-4-21

	public static boolean isDateBefore(String date1, String date2) {

		try {

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			// return df.parse(date1).before(df.parse(date2));
			int ii = df.parse(date1).compareTo(df.parse(date2));
			if (ii == 0) {
				return true;
			} else if (ii < 0) {

				return true;
			} else {

				return false;
			}

		} catch (ParseException e) {

			return false;

		}

	}

	// 判断当前时间是否在时间date2之前

	// /时间格式 2005-4-21

	public static boolean isDateBefore(String date2) {

		try {

			Date date1 = new Date();

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			return date1.before(df.parse(date2));

		} catch (ParseException e) {

			return false;

		}

	}

	/**
	 * 将数据库里的时间转换为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param time
	 * @return
	 */
	public static String getShowDate(String time) {
		return "20" + time.substring(0, 2) + "-" + time.substring(2, 4) + "-"
				+ time.substring(4);
	}

}
